package chen.web.user;

import java.util.List;

import chen.web.event.EventSupport;
import chen.web.user.event.RankChangedEvent;
import chen.web.user.event.RegisterEvent;
import chen.web.user.event.RoleChangedEvent;

public class AccountManagerEventFirer implements AccountManager{

	private AccountManager am;
	private EventSupport es;
	public AccountManagerEventFirer(AccountManager am, EventSupport es){
		this.am = am;
		this.es = es;
	}
	
	@Override
	public Account changeEmail(Account account, String newEmail)
			throws EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		return am.changeEmail(account, newEmail);
	}

	@Override
	public Account changeEmail(String email, String password)
			throws InvalidPasswordException,
			EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		return am.changeEmail(email, password);
	}

	@Override
	public Account changePassword(String newPassword, String oldPassword)
			throws InvalidPasswordException {
		// TODO Auto-generated method stub
		return am.changePassword(newPassword, oldPassword);
	}

	@Override
	public Account changePassword(Account account, String newPassword) {
		// TODO Auto-generated method stub
		return am.changePassword(account, newPassword);
	}

	@Override
	public Account changeRole(Account account, Role role) {
		// TODO Auto-generated method stub
		Role old = account.getRole();
		Account acc = am.changeRole(account, role);
		RoleChangedEvent event = new RoleChangedEvent(acc, old);
		es.fireEvent(event);
		return acc;
		
	}

	@Override
	public void deleteAccount(Account account) {
		// TODO Auto-generated method stub
		am.deleteAccount(account);
	}

	@Override
	public Account getAccount(int id) {
		// TODO Auto-generated method stub
		return am.getAccount(id);
	}

	@Override
	public List<Account> getAccount(List<Integer> ids) {
		// TODO Auto-generated method stub
		return am.getAccount(ids);
	}

	@Override
	public Account getAccountByEmail(String email) {
		// TODO Auto-generated method stub
		return am.getAccountByEmail(email);
	}

	@Override
	public Account getAccountByName(String nickname) {
		// TODO Auto-generated method stub
		return am.getAccountByName(nickname);
	}

	@Override
	public Account increaseRank(Account account, int increase) {
		// TODO Auto-generated method stub
		Account acc = am.increaseRank(account, increase);
		RankChangedEvent event = new RankChangedEvent(acc, acc.getRank() - increase);
		es.fireEvent(event);
		return acc;
	}

	@Override
	public void mergeAccount(Role oldRole, Role newRole) {
		// TODO Auto-generated method stub
		am.mergeAccount(oldRole, newRole);
	}

	@Override
	public Account register(String nickname, String password, String email)
			throws NicknameAlreadyBeenRegisteredException,
			EmailAlreadyBeenRegisteredException {
		// TODO Auto-generated method stub
		Account account =  am.register(nickname, password, email);
		RegisterEvent event = new RegisterEvent(account);
		es.fireEvent(event);
		return account;
	}

	@Override
	public int totalAccount() {
		// TODO Auto-generated method stub
		return am.totalAccount();
	}

	@Override
	public void update(Account account) {
		// TODO Auto-generated method stub
		am.update(account);
	}

}
